import {
    _debounce
} from "@src/common/utils"
import {
    getHistory
} from "@src/api/invoice";
import {
    storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
module.exports = Behavior({
    behaviors: [storeBindingsBehavior],
    storeBindings: [{
        store: base,
        fields: ['themeColor', 'themeTitleColor']
    }],
    data: {
        slots: [{
                text: '商城',
                responseSts: 'shangchengSts',
                flag: 0,
                orderType: 5,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '预约',
                responseSts: 'yuyueSts',
                flag: 0,
                orderType: 17,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '预订',
                responseSts: 'yudingSts',
                flag: 0,
                orderType: 7,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '点餐',
                responseSts: 'diancanSts',
                flag: 0,
                orderType: 6,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '赠送会员',
                responseSts: 'zengsonghuiyuanSts',
                flag: 0,
                orderType: 9,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '会员充值',
                responseSts: 'huiyuanchongzhiSts',
                flag: 0,
                orderType: 2,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }, {
                text: '会员开卡',
                responseSts: 'huiyuankaikaSts',
                flag: 0,
                orderType: 4,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            },
            {
                text: '会员续费',
                responseSts: 'huiyuanxufeiSts',
                flag: 0,
                orderType: 10,
                orderData: [],
                sysInstanceIndex: 1,
                sysInstanceTotalPages: 0
            }
        ],
        currentIndex: 0, //当前处于哪一页
        orderType: 5, // 当前这一页的orderType
        isRefresher: false,
        loading: true,
        themeColorRgba:null
    },
    ready: function () {
        const themeColorRgba =  this.colorTransform(this.data.themeColor)
        this.setData({
            themeColorRgba
        })
    },
    methods: {
        loadMore(e) {
            console.log(e.detail);
            const {
                slots,
                currentIndex,
                orderType
            } = this.data
            if (slots[currentIndex].sysInstanceIndex >= slots[currentIndex].sysInstanceTotalPages) return
            slots[currentIndex].sysInstanceIndex++
            this.tabChangeApi(orderType)
        },
        
        async tabChangeApi(orderType,openFapiaoStatus) {
            const {
                slots,
                currentIndex
            } = this.data
            let {
                code,
                data: {
                    list = [],
                    total
                }
            } = await this.selectTab(openFapiaoStatus ? 1 : slots[currentIndex].sysInstanceIndex, 10, orderType)
            if (code !== 0) return
            slots[currentIndex].sysInstanceTotalPages = Math.ceil(total / 10)
            let fileBaseUrl = getApp().globalData.extConfig.fileBaseUrl;
            !list && (list = [])
            list && list.forEach(item => {
                if (item) {
                    item.isChecked = false
                    item.goodsDtlVos && item.goodsDtlVos.forEach(ele => ele.coverImg = fileBaseUrl + ele.coverImg)
                }
            })
            if(openFapiaoStatus){
                this.setData({
                    ['slots[' + currentIndex + '].orderData']: [...list]
                })
            }else{
                this.setData({
                    ['slots[' + currentIndex + '].orderData']: [...slots[currentIndex].orderData, ...list]
                })
            }
        },
        loadData: _debounce(function (e) {
            const {
                slots,
                currentIndex
            } = this.data
            this.setData({
                money: null,
                isChecked: false,
                ['slots[' + currentIndex + '].orderData']: [],
                ['slots[' + currentIndex + '].sysInstanceIndex']: 1,
                ['slots[' + currentIndex + '].sysInstanceTotalPages']: 0
            })
            this.tabChangeApi(slots[currentIndex].orderType).then(res => {
                setTimeout(() => {
                    this.setData({
                        isRefresher: !this.data.isRefresher
                    })
                }, 500)
            }).catch(err => {
                setTimeout(() => {
                    this.setData({
                        isRefresher: !this.data.isRefresher
                    })
                }, 500)
            })
        }, 500),
        changeTab(e) {
            const {
                slots,
                currentIndex // 老状态
            } = this.data
            let orderData = slots[currentIndex].orderData.slice()
            orderData.forEach(item => {item.isChecked = false })
            const orderType = slots[e.detail].orderType
            this.data.currentIndex = e.detail // 新状态
            this.data.orderType = orderType
            this.setData({
                ['slots[' + currentIndex + '].orderData']: orderData,
                isChecked: false,
                money: null
            })
            !slots[e.detail].orderData.length && this.tabChangeApi(orderType)
        },
        selectTab(pageNum, pageSize, orderType) {
            return new Promise((resolve, reject) => {
                getHistory({
                    pageNum,
                    pageSize,
                    orderType
                }).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                    wx.showToast({
                        title: err.msg,
                        icon: 'none',
                        duration: 2000
                    })
                })
            })
        },
        colorTransform(sHex, alpha = 0.14) {
            let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
            /* 16进制颜色转为RGB格式 */
            let sColor = sHex.toLowerCase()
            if (sColor && reg.test(sColor)) {
                if (sColor.length === 4) {
                    let sColorNew = '#'
                    for (let i = 1; i < 4; i += 1) {
                        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
                    }
                    sColor = sColorNew
                }
                //  处理六位的颜色值
                let sColorChange = []
                for (let i = 1; i < 7; i += 2) {
                    // eslint-disable-next-line radix
                    sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`))
                }
                return `rgba(${sColorChange.join(',')},${alpha})`
            }
            return sColor
        }
    }
})